package com.demo;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;

import com.itextpdf.layout.font.FontProvider;

import java.io.*;

public class HtmlToPdf {
    public static void main(String[] args) throws IOException {
        String htmlContent = "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "    <meta charset=\"utf-8\"/>\n" +
                "    <title></title>\n" +
                "    <style>\n" +
                "        *{\n" +
                "            margin: 0;\n" +
                "            padding: 0;\n" +
                "            box-sizing: border-box;\n" +
                "        }\n" +
                "        body{\n" +
                "            font-family: SimSun;\n" +
                "\t\t\t  color: chartreuse;\n" +
                "        }\n" +
                "        section{\n" +
                "            display:block;\n" +
                "            margin: 20px 10px;\n" +
                "        }\n" +
                "        .title{\n" +
                "            text-align: center;\n" +
                "        }\n" +
                "        .preface p{\n" +
                "            line-height: 30px;\n" +
                "        }\n" +
                "        .preface p.content{\n" +
                "            text-indent: 2em;\n" +
                "        }\n" +
                "        section > table{\n" +
                "            table-layout: fixed;\n" +
                "            width: 100%;\n" +
                "            margin: 20px 0px;\n" +
                "            text-align:center;\n" +
                "            word-wrap:break-word;\n" +
                "        }\n" +
                "        section table td{\n" +
                "            padding:5px 0px;\n" +
                "        }\n" +
                "        .header span{\n" +
                "            border-bottom: 1px solid;\n" +
                "            padding: 5px;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "<!-- 标题 start -->\n" +
                "<section class=\"title\">\n" +
                "    <h2>报告大人</h2>\n" +
                "</section>\n" +
                "<!-- 标题 end -->\n" +
                "\n" +
                "<!-- 前言 start -->\n" +
                "<div>\n" +
                "    <span>加油啊</span>\n" +
                "</div>\n" +
                "<!-- 前言 end -->\n" +
                "\n" +
                "<!-- 汇总统计信息 start -->\n" +
                "<section class=\"count-info\">\n" +
                "    <h4>汇总统计信息</h4>\n" +
                "    <table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n" +
                "        <tr>\n" +
                "            <td>本月笔数</td>\n" +
                "            <td>近三个月数量对比</td>\n" +
                "        </tr>\n" +
                "        <tr>\n" +
                "            <td>${curr}</td>\n" +
                "            <td>\n" +
                "                <table width=\"80%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" style=\"margin: 5px auto;\">\n" +
                "                    <tr>\n" +
                "                        <td>${one}</td>\n" +
                "                        <td>${two}</td>\n" +
                "                        <td>${three}</td>\n" +
                "                    </tr>\n" +
                "                </table>\n" +
                "            </td>\n" +
                "        </tr>\n" +
                "    </table>\n" +
                "</section>\n" +
                "<!-- 汇总统计信息 end -->\n" +
                "\n" +
                "\n" +
                "<!-- 明细 start -->\n" +
                "<section class=\"detail\">\n" +
                "    <h4>明细</h4>\n" +
                "    <table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n" +
                "        <tr>\n" +
                "            <td width=\"5%\">序号</td>\n" +
                "            <td width=\"15%\">列1</td>\n" +
                "            <td width=\"12%\">列2</td>\n" +
                "            <td width=\"12%\">列3</td>\n" +
                "            <td width=\"12%\">列4</td>\n" +
                "            <td>列5</td>\n" +
                "        </tr>\n" +
                "        <#list detailList as ad>\n" +
                "            <tr>\n" +
                "                <td>${ad_index+1}</td>\n" +
                "                <td>${ad.column1}</td>\n" +
                "                <td>${ad.column2}</td>\n" +
                "                <td>${ad.column3}</td>\n" +
                "                <td>${ad.column4}</td>\n" +
                "                <td>${ad.column5}</td>\n" +
                "            </tr>\n" +
                "        </#list>\n" +
                "    </table>\n" +
                "</section>\n" +
                "\n" +
                "<section>\n" +
                "    <form action=\"\" method=\"get\">\n" +
                "        您喜欢的水果？<br /><br />\n" +
                "        <label><input name=\"Fruit\" type=\"checkbox\" checked=checked>苹果 </label>\n" +
                "        <label><input name=\"Fruit\" type=\"checkbox\" value=\"\" />桃子 </label>\n" +
                "        <label><input name=\"Fruit\" type=\"checkbox\" value=\"\" />香蕉 </label>\n" +
                "        <label><input name=\"Fruit\" type=\"checkbox\" value=\"\" />梨 </label>\n" +
                "    </form>\n" +
                "</section>\n" +
                "<!-- 明细 end -->\n" +
                "</body>\n" +
                "</html>";
        String pdfPath = "D:\\docker-images\\666.pdf";

        ConverterProperties properties = new ConverterProperties();
        //添加中文字体支持
        FontProvider fontProvider = new FontProvider();
        PdfFont sysFont = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
        fontProvider.addFont(sysFont.getFontProgram(), "UniGB-UCS2-H");
        properties.setFontProvider(fontProvider);

        try {
            HtmlConverter.convertToPdf(htmlContent, new FileOutputStream(new File(pdfPath)), properties);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
